/* 
 * (C) 2013 Daniel Garcia <contacto {at} danigarcia.org>
 * 
 * http://danigarcia.org
 * 
 * This file is part of BlackList.
 * 
 * BlackList is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BlackList is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BlackList.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.danigarcia.android.blacklist.database;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class ConfigManager {

	private static final String SHARED_PREFERENCES = "config";

	public static final String KEY_STATUS				= "status";
	public static final String KEY_SERVICE				= "service";
	public static final String KEY_PICKUP				= "pickup";
	public static final String KEY_FILTER_SMS			= "filterSms";
	public static final String KEY_FILTER_SMS_BODY		= "filterSmsBody";
	public static final String KEY_SAVE_SMS				= "saveSms";
	public static final String KEY_SHOW_NOTIFICATIONS	= "showNotifications";

	public static final boolean 	DEBUG_LOGGING	= true;
	public static final boolean 	ERROR_LOGGING	= true;
	public static final boolean 	WARNING_LOGGING	= true;
	public static final boolean 	INFO_LOGGING	= true;
	public static final boolean 	VERBOSE_LOGGING	= true;
	
	private static SharedPreferences preferences;
	
	public static boolean isActive()
	{
		return preferences != null;
	}
	
	public static void init(Context context)
	{
		preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
	}
	
	public static SharedPreferences getPreferences(Context context)
	{
		if(preferences == null)
			 preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
		
		return preferences;
	}
	
	public static String getString(String key)
	{
		if(preferences != null)
			return preferences.getString(key, null);
		
		return null;
	}
	
	public static int getInt(String key)
	{
		if(preferences != null)
			return preferences.getInt(key, -1);
		
		return -1;
	}
	
	public static boolean getBoolean(String key)
	{
		if(preferences != null)
			return preferences.getBoolean(key, false);
		
		return false;
	}
	
	public static boolean insert(String key, String value)
	{
		if(preferences != null)
		{
			Editor editor = preferences.edit();
			editor.putString(key, value);
			return editor.commit();
		}
		
		return false;
	}
	
	public static boolean insert(String key, int value)
	{
		if(preferences != null)
		{
			Editor editor = preferences.edit();
			editor.putInt(key, value);
			return editor.commit();
		}
		
		return false;
	}
	
	public static boolean insert(String key, long value)
	{
		if(preferences != null)
		{
			Editor editor = preferences.edit();
			editor.putLong(key, value);
			return editor.commit();
		}
		
		return false;
	}
	
	public static boolean insert(String key, float value)
	{
		if(preferences != null)
		{
			Editor editor = preferences.edit();
			editor.putFloat(key, value);
			return editor.commit();
		}
		
		return false;
	}
	
	public static boolean insert(String key, boolean value)
	{
		if(preferences != null)
		{
			Editor editor = preferences.edit();
			editor.putBoolean(key, value);
			return editor.commit();
		}
		
		return false;
	}
	
	public static void dispose()
	{
		if(preferences != null)
			preferences = null;
	}
}
